package com.api.oa.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.api.oa.utils.LocalRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class EmServer {

    @Autowired
    LocalRedis localRedis;


    /**
     * 环信token
     *
     * @return
     */
    public String getImToken() {
        Object o = localRedis.get("im.accessToken");
        if (o == null) {
            String url = String.format("https://a1.easemob.com/1163220307110014/jiaozhou/token");
            cn.hutool.json.JSONObject obj = JSONUtil.createObj();
            obj.set("grant_type", "client_credentials");
            obj.set("client_id", "YXA6F5vtc-EiQh6ngaHs8K7hQg");
            obj.set("client_secret", "YXA6fSh2zPmwN--l8RYEuYrKJj3hec4");
            obj.set("ttl", DateUtil.currentSeconds());
            String body = HttpRequest.post(url).body(obj.toString(), "application/json").execute().body();
            cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(body);
            String access_token = jsonObject.getStr("access_token");
            localRedis.set("im.accessToken", access_token, 5184000);
            o = access_token;
        }
        return (String) o;
    }

    public String createImUser(String username, String password, String nickName) {

        String url = String.format("https://a1.easemob.com/1163220307110014/jiaozhou/users");
        cn.hutool.json.JSONObject obj = JSONUtil.createObj();
        obj.set("username", username);
        obj.set("password", password);
        obj.set("nickname", nickName);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer " + getImToken());
        String body = HttpRequest.post(url).body(obj.toString(), "application/json").addHeaders(headers).execute().body();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(body);
        JSONArray entities = jsonObject.getJSONArray("entities");
        if(entities == null)return null;
        JSONObject o = (JSONObject) entities.get(0);
        return o.getStr("uuid");
    }
    public JSONArray createImUserBatch(JSONArray jsonArray) {

        String url = String.format("https://a1.easemob.com/1163220307110014/jiaozhou/users");

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer " + getImToken());
        String body = HttpRequest.post(url).body(jsonArray.toString(), "application/json").addHeaders(headers).execute().body();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(body);
        JSONArray entities = jsonObject.getJSONArray("entities");
        return entities;
    }

    /**
     * 修改别名
     * @param username
     * @param nickName
     * @return
     */
    public String updateImUser(String username, String nickName) {

        String url = String.format("https://a1.easemob.com/1163220307110014/jiaozhou/metadata/user/" + username);
        Map<String,Object> obj = new HashMap<>();
        obj.put("name", nickName);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("Authorization", "Bearer " + getImToken());
        String body = HttpRequest.put(url).form(obj).addHeaders(headers).execute().body();
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(body);
        return body;
    }




}
